/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package familytree.struts.actions;

import familytree.negocio.IArbolProcessor;
import familytree.negocio.INodoProcessor;
import familytree.negocio.bo.ArbolBO;
import familytree.negocio.bo.NodoBO;
import familytree.negocio.bo.ParejaBO;
import familytree.negocio.bo.UsuarioBO;
import familytree.persistencia.pojos.Arbol;
import familytree.persistencia.pojos.Imagen;
import familytree.persistencia.pojos.Nodo;
import familytree.persistencia.pojos.Usuario;
import familytree.struts.forms.PrimerArbolForm;
import familytree.util.ITree;
import java.io.File;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;

/**
 *
 * @author Antonio
 */
public class AltaFamiliaAction extends Action {
    
    /* forward name="ok" path="" */
    private final static String OK = "ok";

    /*forward name = "fallo" path=""*/
    private final static String FALLO = "fallo";
    
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    public ActionForward execute(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        //Recogemos los datos del formulario
        PrimerArbolForm arbolForm = (PrimerArbolForm)form;
         
        //Obtenemos los datos de la familia (Arbol) y de sus componentes (Nodos)
        Arbol arbol = arbolForm.getArbol();
        Nodo nodoHijo = arbolForm.getNodo();
        Nodo nodoPadre = arbolForm.getPadre();
        Nodo nodoMadre = arbolForm.getMadre();
        Usuario usuario = (Usuario)request.getSession().getAttribute("usuarioActual");
        request.getSession().setAttribute("nodoActual", nodoHijo);
        //Almacenamos el árbol
        IArbolProcessor arbolBD = new ArbolBO();
        
        usuario = arbolBD.iniciarFamilia(arbol,nodoHijo,nodoPadre,nodoMadre,usuario);
       
        //nodoHijo.setPadres(padres);
        INodoProcessor nodoBd = new  NodoBO();
        Nodo nodoRaiz = nodoBd.getNodoPorIdNodo(usuario.getNodo().getIdNodo());
        IArbolProcessor arbolProcessor = new ArbolBO();
        ITree[] familia = arbolProcessor.createTrees(nodoRaiz);
        request.getSession().setAttribute("familia", familia);
        String[][] localizacion = arbolProcessor.getruta(nodoRaiz,familia);
        request.getSession().setAttribute("localizacion", localizacion);
        
        return mapping.findForward(OK);
        
    }
}